# Python 2.6.4
# Project Euler, Problem 162
# Copyright 2010 Talha Zaman

memo = dict()
def seen(left, allow=False, Z=0, O=0, A=0):
    if allow and memo.has_key((left, Z, O, A)): return memo[left, Z, O, A]
    elif Z and O and A: s = 16**left
    elif left==0: s = 0
    else:
        s  = seen(left-1, True, 1, O, A) if allow else 0
        s += seen(left-1, True, Z, 1, A)
        s += seen(left-1, True, Z, O, 1)
        s += seen(left-1, True, Z, O, A) * 13
    if allow: memo[left, Z, O, A] = s
    return s
print str(hex(sum(seen(i) for i in range(17)))).upper()[2:-1]
